package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jt.service.UserService;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
public class UserController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private JedisCluster jedisCluster;
	
	@RequestMapping("/findUserAll")
	public List<User> findUserAll(){
		return userService.findUserAll();
	}

	@RequestMapping("/user/check/{param}/{type}")
	public JSONPObject checkUser(@PathVariable String param,@PathVariable Integer type,String callback){
		boolean flag=userService.checkuser(param,type);
		SysResult.success(flag);
		return new JSONPObject(callback, SysResult.success(flag));
	}

	@RequestMapping("/user/query/{ticket}")
	public JSONPObject findUserByTicket(@PathVariable String ticket,
										HttpServletResponse response,
										String callback){

		String userJSON = jedisCluster.get(ticket);
		//1.lru算法清空数据   2.有可能cookie信息有误
		if(StringUtils.isEmpty(userJSON)){
			//2.应该删除cookie信息.
			Cookie cookie = new Cookie("JT_TICKET", "");
			cookie.setMaxAge(0);
			cookie.setDomain("jt.com");
			cookie.setPath("/");
			response.addCookie(cookie);
			return new JSONPObject(callback,SysResult.fail());
		}
		return new JSONPObject(callback,SysResult.success(userJSON));
	}
}
